package com.ltkj.ticket.service.impl;

import com.ltkj.common.core.exception.ServiceException;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.core.utils.ListUtiles;
import com.ltkj.common.core.utils.StringUtils;
import com.ltkj.common.datascope.annotation.DataScope;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.ticket.domain.TicVenues;
import com.ltkj.ticket.domain.TicVenuesConfig;
import com.ltkj.ticket.domain.TicVenuesPeriod;
import com.ltkj.ticket.domain.vo.TicVenuesAddVO;
import com.ltkj.ticket.domain.vo.TicVenuesListVO;
import com.ltkj.ticket.mapper.TicVenuesBusinessMapper;
import com.ltkj.ticket.mapper.TicVenuesConfigMapper;
import com.ltkj.ticket.mapper.TicVenuesMapper;
import com.ltkj.ticket.mapper.TicVenuesPeriodMapper;
import com.ltkj.ticket.service.iTicVenuesBusinessService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @Author gushaoliang
 * @Date 2023/8/30 14:55
 * @PackageName:com.ltkj.ticket.service.impl
 * @ClassName: TicVenuesBusinessServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class TicVenuesBusinessServiceImpl implements iTicVenuesBusinessService {
    private static final Logger log = LoggerFactory.getLogger(TicVenuesBusinessServiceImpl.class);
    @Autowired
    TicVenuesBusinessMapper ticVenuesBusinessMapper;
    @Autowired
    private TicVenuesMapper ticVenuesMapper;
    @Autowired
    private TicVenuesConfigMapper ticVenuesConfigMapper;
    @Autowired
    private TicVenuesPeriodMapper ticVenuesPeriodMapper;

    /*
     * @description:场馆门票列表
     * @author: gushaoliang
     * @date: 2023/8/30 16:35
     * @param: [ticVenuesListVO]
     * @return: java.util.List<com.ltkj.ticket.domain.vo.TicVenuesListVO>
     **/
    @Override
    @DataScope(storeAlias = "g")
    public List<TicVenuesListVO> list(TicVenuesListVO ticVenuesListVO) {

        return ticVenuesBusinessMapper.list(ticVenuesListVO);
    }

    /*
     * @description:场馆门票下架of上架
     * @author: gushaoliang
     * @date: 2023/8/30 16:43
     * @param: [ticVenues]
     * @return: int
     **/
    @Override
    public int editVenuesStatus(TicVenues ticVenues) {
        ticVenues.setUpdateTime(DateUtils.getNowDate());
        String username = SecurityUtils.getUsername();
        ticVenues.setUpdateBy(username);
        return ticVenuesMapper.updateTicVenues(ticVenues);
    }
    /*
     * @description:场馆门票下架
     * @author: gushaoliang
     * @date: 2023/8/30 16:43
     * @param: [ticVenues]
     * @return: int
     **/
    @Override
    public int offVenues(Long venuesId) {
        TicVenues ticVenues = ticVenuesMapper.selectTicVenuesByVenuesId(venuesId);
        if ("1".equals(ticVenues.getIsDel())) {
            throw new ServiceException("数据不存在");
        }
        ticVenues.setUpdateTime(DateUtils.getNowDate());
        String username = SecurityUtils.getUsername();
        ticVenues.setUpdateBy(username);
        ticVenues.setVenuesStatus("1");
        return ticVenuesMapper.updateTicVenues(ticVenues);
    }
    /*
     * @description:场馆门票上架
     * @author: gushaoliang
     * @date: 2023/8/30 16:43
     * @param: [ticVenues]
     * @return: int
     **/
    @Override
    public int onVenues(Long venuesId) {
        TicVenues ticVenues = ticVenuesMapper.selectTicVenuesByVenuesId(venuesId);
        if ("1".equals(ticVenues.getIsDel())) {
            throw new ServiceException("数据不存在");
        }
        ticVenues.setUpdateTime(DateUtils.getNowDate());
        String username = SecurityUtils.getUsername();
        ticVenues.setUpdateBy(username);
        ticVenues.setVenuesStatus("0");
        return ticVenuesMapper.updateTicVenues(ticVenues);
    }

    /*
     * @description:新增场馆门票
     * @author: gushaoliang
     * @date: 2023/8/30 16:58
     * @param: [ticVenuesAddVO]
     * @return: int
     **/
    @Override
    @Transactional
    public int addVenuesBusiness(TicVenuesAddVO ticVenuesAddVO) {
        // 新增场馆门票时间段配置表
        // 判断选择开始时间与结束时间的间隔是几天


        List<String> ascDateList = DateUtils.getAscDateList(ticVenuesAddVO.getStartTime(), ticVenuesAddVO.getEndTime());
        // 判断是否存在当前日期、当前场馆的信息

        int i = ticVenuesBusinessMapper.searchCountBydateAndVenues(ascDateList, ticVenuesAddVO.getTicOtherBaseId());
        if (i > 0) {
            log.info("新增场馆门票存在当前日期当前场馆的数据");
            throw new ServiceException("新增场馆门票存在当前日期当前场馆的数据,请检查");
        }
        //新增门票信息

        TicVenues ticVenues = ticVenuesMapper.selectTicVenuesByVenuesId(ticVenuesAddVO.getVenuesId());

        ticVenues.setVenuesProgrammeId(ticVenuesAddVO.getVenuesProgrammeId());
        ticVenues.setUpdateTime(DateUtils.getNowDate());
        String username = SecurityUtils.getUsername();
        ticVenues.setUpdateBy(username);
        int i1 = ticVenuesMapper.updateTicVenues(ticVenues);




        // 批量新增
        List<List<String>> list = ListUtiles.dealData(ascDateList, 5000);
        // 新增门票时间段表
        List<TicVenuesPeriod> venuesPeriodList = ticVenuesAddVO.getVenuesPeriodList();
        int num = 0;
        for (int b = 0; b < list.size(); b++) {
            // 批量新增场馆门票日期配置表
            num = ticVenuesBusinessMapper.inserTvenuesConfigList(list.get(b), username, ticVenues.getVenuesId(), ticVenuesAddVO.getVenuesProgrammeId(), ticVenuesAddVO.getTicOtherBaseId(),ticVenuesAddVO.getSysStoresId());
            // 批量新增门票时间段表
            ticVenuesBusinessMapper.inserVenuesPeriodList(list.get(b), venuesPeriodList, username, ticVenues.getVenuesId(), ticVenuesAddVO.getVenuesProgrammeId(), ticVenuesAddVO.getTicOtherBaseId());

        }
        return num;
    }

    @Override
    public TicVenuesAddVO getVenuesConfigInfo(Long ticVenuesConfigId) {
        TicVenuesAddVO ticVenuesAddVO = new TicVenuesAddVO();

        TicVenuesConfig ticVenuesConfig = ticVenuesConfigMapper.selectTicVenuesConfigByTicVenuesConfigId(ticVenuesConfigId);
        ticVenuesAddVO.setVenuesId(ticVenuesConfig.getVenuesId());
        ticVenuesAddVO.setTicOtherBaseId(ticVenuesConfig.getTicOtherBaseId());
        ticVenuesAddVO.setVenuesProgrammeId(ticVenuesConfig.getVenuesProgrammeId());
        ticVenuesAddVO.setTicVenuesConfigId(ticVenuesConfig.getTicVenuesConfigId());
        ticVenuesAddVO.setVenuesDate(ticVenuesConfig.getVenuesDate());
        // if(("1").equals(ticVenuesConfig.getIsDel())){
        //     log.info("当前日期无配置");
        //     throw new ServiceException("当前日期无配置信息");
        // }
        // 查询方案信息
        TicVenues ticVenues = ticVenuesMapper.selectTicVenuesByVenuesId(ticVenuesConfig.getVenuesId());
        ticVenuesAddVO.setTicVenues(ticVenues);
        // 查询时间段信息
        TicVenuesPeriod ticVenuesPeriod = new TicVenuesPeriod();
        ticVenuesPeriod.setVenuesDate(ticVenuesConfig.getVenuesDate());
        ticVenuesPeriod.setTicOtherBaseId(ticVenuesConfig.getTicOtherBaseId());
        ticVenuesPeriod.setIsDel("0");
        List<TicVenuesPeriod> ticVenuesPeriods = ticVenuesPeriodMapper.selectTicVenuesPeriodList(ticVenuesPeriod);
        ticVenuesAddVO.setVenuesPeriodList(ticVenuesPeriods);

        return ticVenuesAddVO;
    }

    @Override
    public int editVenuesConfigInfo(TicVenuesAddVO ticVenuesAddVO) {
        String username = SecurityUtils.getUsername();
        // TicVenues ticVenues = ticVenuesAddVO.getTicVenues();
        // ticVenues.setVenuesId(ticVenuesAddVO.getVenuesId());
        // ticVenues.setUpdateTime(DateUtils.getNowDate());
        // String username = SecurityUtils.getUsername();
        // ticVenues.setUpdateBy(username);
        // ticVenuesMapper.updateTicVenues(ticVenues);
        if(ticVenuesAddVO.getVenuesDate().before(DateUtils.getNowDate())){
            throw new ServiceException("历史库存不能修改");
        }

        TicVenuesConfig ticVenuesConfig = ticVenuesConfigMapper.selectTicVenuesConfigByTicVenuesConfigId(ticVenuesAddVO.getTicVenuesConfigId());
        if (StringUtils.isNull(ticVenuesConfig)) {
            throw new ServiceException("门票配置信息不存在");
        }
        // ticVenuesConfig.setPeriodStockCount(ticVenuesAddVO.getPeriodStockCount());
        ticVenuesConfig.setUpdateBy(username);
        ticVenuesConfig.setUpdateTime(DateUtils.getNowDate());
        int i = ticVenuesConfigMapper.updateTicVenuesConfig(ticVenuesConfig);
        // 修改配置信息
        // 先删除
        //先查询当前场馆门票的时间段
        List<TicVenuesPeriod> xinzengList=new ArrayList<>();
        List<TicVenuesPeriod> xiugaiList=new ArrayList<>();
        List<TicVenuesPeriod> ticVenuesPeriods = ticVenuesBusinessMapper.searchList(ticVenuesConfig.getTicOtherBaseId(), ticVenuesConfig.getVenuesDate());
       if(ticVenuesAddVO.getVenuesPeriodList()!=null&&ticVenuesAddVO.getVenuesPeriodList().size()>0){
           for(int b=0;b<ticVenuesAddVO.getVenuesPeriodList().size();b++){
               if(ObjectUtils.isNotEmpty(ticVenuesAddVO.getVenuesPeriodList().get(b).getPeriodId())){
                   //查询是否存在
                   TicVenuesPeriod ticVenuesPeriod = ticVenuesPeriodMapper.selectTicVenuesPeriodByPeriodId(ticVenuesAddVO.getVenuesPeriodList().get(b).getPeriodId());
                   if(ticVenuesPeriod!=null){
                       //修改的数据
                       Long periodStockCount = ticVenuesAddVO.getVenuesPeriodList().get(b).getPeriodStockCount();//修改后的总库存
                       Long periodStockCount1 = ticVenuesPeriod.getPeriodStockCount();//原库存
                       Long periodStockResidue = ticVenuesPeriod.getPeriodStockResidue();//原剩余库存

                       if(periodStockCount<periodStockResidue){
                           throw new ServiceException("库存不能小于生育库存");

                       }
                       long periodStockResidue1 = periodStockCount - (periodStockCount1 - periodStockResidue);//修改后剩余库存
                       ticVenuesAddVO.getVenuesPeriodList().get(b).setPeriodStockResidue(periodStockResidue1);

                       xiugaiList.add(ticVenuesAddVO.getVenuesPeriodList().get(b));
                   }

               }else {
                   //新增的数据
                   xinzengList.add(ticVenuesAddVO.getVenuesPeriodList().get(b));
               }

           }
           //所有id
           List<Long> collect = ticVenuesPeriods.stream().map(TicVenuesPeriod::getPeriodId).collect(toList());
           //入参id
           List<Long> rucan = ticVenuesAddVO.getVenuesPeriodList().stream().map(TicVenuesPeriod::getPeriodId).collect(toList());
           List<Long> reduce1 = collect.stream().filter(item -> !rucan.contains(item)).collect(toList());

           //修改此数据删除数据为已删除
           if(reduce1!=null&&reduce1.size()>0){
               for(int c=0;c<reduce1.size();c++){
                   ticVenuesBusinessMapper.updateVenuesPeriodById(reduce1.get(c),username);
               }

           }

           //新增数据
           if(xinzengList!=null&&xinzengList.size()>0){
               ticVenuesBusinessMapper.inserVenuesPeriodEdit(xinzengList, username,
                       ticVenuesConfig.getVenuesId(), ticVenuesConfig.getVenuesProgrammeId(), ticVenuesConfig.getTicOtherBaseId(), ticVenuesConfig.getVenuesDate());
           }
           if(xiugaiList!=null&&xiugaiList.size()>0){
               //修改的数据
               for(int d=0;d<xiugaiList.size();d++){
                   ticVenuesBusinessMapper.updatePeriodEdit(xiugaiList.get(d), username,
                           ticVenuesConfig.getVenuesId(), ticVenuesConfig.getVenuesProgrammeId(), ticVenuesConfig.getTicOtherBaseId(), ticVenuesConfig.getVenuesDate());
               }

           }



       }else {
           if(ticVenuesPeriods!=null&&ticVenuesPeriods.size()>0){

           }else {
               int i1 = ticVenuesBusinessMapper.deleteVenuesPeriodByDateAndOtherBaseId(ticVenuesConfig.getTicOtherBaseId(), ticVenuesConfig.getVenuesDate());
           }


       }

        return i;
    }
}